<?php
    header('Content-Type: text/html; charset=UTF-8');
    include_once '../../config.php';

    mysql_query("SET NAMES 'utf8'");
    mysql_query("SET CHARACTER SET 'utf8'");

    //Projektnummer generieren
    $sql = "SELECT * FROM project WHERE year = '" . date('Y') . "' AND month = '" . date('m') . "' ORDER BY ym_p_nr DESC LIMIT 1";
    $result = mysql_query($sql);

    if (mysql_num_rows($result) > 0) {
        $data = mysql_fetch_array($result);
        $project_nr_single = $data['ym_p_nr'] + 1;
        if ($project_nr_single < 10) {
            $project_nr_single = "0" . $project_nr_single;
        }
        $project_nr = date('Y-m'). '-' . $project_nr_single;
    } else {
        $project_nr = date('Y-m'). '-01';
        $project_nr_single = "01";
    }


    //Datum verteile
    if (empty($_POST['p_date'])) {
        $p_date = date('Y-m-d');
    } else {
        $p_date = $_POST['p_date'];
    }

    //Restlichen Variablen
    $p_name    = $_POST['p_name'];
    $p_street  = $_POST['p_street'];
    $p_zip     = $_POST['p_zip'];
    $p_city    = $_POST['p_city'];
    $p_client  = $_POST['p_client_list'];

    if (!$p_name || !$p_city) {
        header('Location: ../add.php?ag=pname');
    } else {
        if ($p_client == 'no') {
            header('Location: ../add.php?ag=no');
        } else if ($p_client == 'new') {
            $c_name   = $_POST['client_name'];
            $c_street = $_POST['client_street'];
            $c_zip    = $_POST['client_zip'];
            $c_city   = $_POST['client_city'];

            if ($c_name == '' || $c_street  == '' || $c_zip  == '' || $c_city == '') {
                header('Location: ../add.php?ag=newno');
            } else {
                $sql = "INSERT INTO address(street,zip,city) VALUES('" . $c_street . "','" . $c_zip . "','" . $c_city  . "')";
                mysql_query($sql) or die (mysql_error());
                $sql = "INSERT INTO client(address_id,name) VALUES('" . mysql_insert_id() . "','" . $c_name . "')";
                mysql_query($sql) or die (mysql_error());
                $p_client = mysql_insert_id();

                $sql = "INSERT INTO address(street,zip,city) VALUES('" . $p_street . "','" . $p_zip . "','" . $p_city  . "')";
                mysql_query($sql) or die (mysql_error());
                $sql = "INSERT INTO project(project_nr,ym_p_nr,address_id,p_name,client_id,date_inquiry,year,month) VALUES"
                     . "('" . $project_nr . "','" . $project_nr_single . "','" . mysql_insert_id() . "','" . $p_name . "',"
                     . "'". $p_client ."','" . $p_date . "','" . date('Y') . "','" . date('m') . "')";
                mysql_query($sql) or die (mysql_error());

                header('Location: ../index.php');
            }
        } else {
            $sql = "INSERT INTO address(street,zip,city) VALUES('" . $p_street . "','" . $p_zip . "','" . $p_city  . "')";
            mysql_query($sql) or die (mysql_error());
            $sql = "INSERT INTO project(project_nr,ym_p_nr,address_id,p_name,client_id,date_inquiry,year,month) VALUES"
                 . "('" . $project_nr . "','" . $project_nr_single . "','" . mysql_insert_id() . "','" . $p_name . "',"
                 . "'". $p_client ."','" . $p_date . "','" . date('Y') . "','" . date('m') . "')";
            mysql_query($sql) or die (mysql_error());

            header('Location: ../index.php');
        }
    }
?>
